<template>
  <div class="cartcontrol">
    <transition name="move">
      <div class="iconfont icon-remove_circle_outline" v-if="food.count" @click.stop="updateFoodCount('sub')"></div>
    </transition>
    <div class="cart-count" v-if="food.count">{{food.count}}</div>
    <div class="iconfont icon-add_circle" @click.stop="updateFoodCount('add')"></div>
  </div>
</template>

<script>
  import Vue from 'vue'
  import {mapState} from 'vuex'
  export default {
    props:{
      food: Object,
    },
    computed: {
      ...mapState(['cartFoods'])
    },
    methods: {
      updateFoodCount(flag) {
        const food = this.food;
        if(flag=='add') {
          if(!food.count) {
            Vue.set(food,'count',1);   // 给有数据绑定的对象添加指定属性名和值的属性(有绑定)
            this.cartFoods.push(food)
          }else {
            food.count ++;
          }
        }else {
          food.count --;
          if(food.count===0) {
            this.cartFoods.splice(this.cartFoods.indexOf(food),1)
          }
        }
        //同步更新food中的count值
        this.$store.commit('FOOD_COUNT');
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/mixins.styl"
  .cartcontrol
    font-size: 0
    .cart-decrease
      display: inline-block
      padding: 6px
      line-height: 24px
      font-size: 24px
      color: rgb(0, 160, 220)

    .icon-remove_circle_outline
      display: inline-block
      padding 6px
      line-height 24px
      font-size 24px
      color $green
      &.move-enter-active, &.move-leave-active
        transition all .3s
      &.move-enter, &.move-leave-to
        opacity 0
        transform translateX(15px) rotate(180deg)
    .cart-count
      display: inline-block
      vertical-align: top
      width: 12px
      padding-top: 6px
      line-height: 24px
      text-align: center
      font-size: 10px
      color: rgb(147, 153, 159)
    .icon-add_circle
      display: inline-block
      padding: 6px
      line-height: 24px
      font-size: 24px
      color $green
</style>
